package com.waves.user.listener;

import com.rabbitmq.client.Channel;
import com.waves.common.domain.dto.PushMessageDTO;
import com.waves.core.constant.MQConstant;
import com.waves.user.domain.enums.WSPushTypeEnum;
import com.waves.websocket.service.WebSocketService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author dlw
 * @email
 */
@Service
public class UserPushListener {

    @Resource
    private WebSocketService webSocketService;

    /**
     * 监听消息推送
     * @param message
     * @param channel
     * @param msg
     * @throws IOException
     */
    @RabbitListener(queues = MQConstant.WEBSOCKET_PUSH_MSG_QUEUE)
    public void listener(PushMessageDTO message, Channel channel, Message msg) throws IOException {
        WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(message.getPushType());
        switch (wsPushTypeEnum) {
            case USER:
                message.getUidList().forEach(uid -> webSocketService.sendToUid(message.getWsBaseMsg(), uid));
                break;
            case ALL:
                webSocketService.sendToAllOnline(message.getWsBaseMsg(), null);
                break;
        }
    }


}
